package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.Channel;
import com.slg.android.manyanime.utils.DataStore;

import java.util.List;

/**
 * Created by E540 on 2015/3/12.
 */
public class AnimeListAdapter extends BaseAdapter {
    private Context context;
    private List<Channel> data;
    private BitmapUtils bitmapUtils;

    public AnimeListAdapter(Context context, List<Channel> data) {
        this.context = context;
        this.data = data;
        bitmapUtils = DataStore.getBitmapUtils();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.
                    from(context).inflate(R.layout.item_anime_channel, parent, false);
        }
        ChannelViewHolder holder = (ChannelViewHolder) convertView.getTag();
        if (holder == null) {
            holder = new ChannelViewHolder();
            convertView.setTag(holder);
        }
        ViewUtils.inject(holder, convertView);
        Channel item = (Channel) getItem(position);
        holder.name.setText(item.getName());
        holder.pic.setImageResource(R.drawable.item_default);
        bitmapUtils.display(holder.pic,item.getPic());
        return convertView;
    }

    public static class ChannelViewHolder {
        @ViewInject(R.id.item_anime_channel_pic_ImageView)
        public ImageView pic;
        @ViewInject(R.id.item_anime_channel_name_TextView)
        public TextView name;
    }
}
